package cn.xianyang.server.impl;

import cn.xianyang.domain.Movie;
import cn.xianyang.mapper.MovieMapper;
import cn.xianyang.server.IMovieServer;
import cn.xianyang.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MovieServerImpl implements IMovieServer {

    @Autowired
    private MovieMapper movieMapper;


    @Override
    public List<Movie> findAll() {
        //查询所有电影信息
        return movieMapper.findAll();
    }

    @Override
    public AjaxResult buyTickets(Long number, Long id) {
        //买票业务，判断是否有余票
        Movie movie = movieMapper.findById(id);
        if(number<=movie.getStock()) {
            movieMapper.buyTickets(number, id);
            return new AjaxResult(true,movie.getName()+number+"张购买成功！！",null);
        }else {
            return new  AjaxResult(false,movie.getName()+"余票不足"+number+"张！！",null);
        }
    }

    @Override
    public Movie findById(Long id) {
        //查询单个电影
        return movieMapper.findById(id);
    }

    @Override
    public Movie findMaxTicket() {
        //查询余票最多的电影 用于注册赠送
        return movieMapper.findMaxTicket();
    }
}
